
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// flutter嵌入安卓原生view

class FlutterInAndroid extends StatefulWidget {
  final String ?text;

  FlutterInAndroid({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return _FlutterInAndroidState();
  }
}

class _FlutterInAndroidState extends State<FlutterInAndroid> {
  //Flutter 向 Android View 发送消息,方法名称必须唯一性，与android那边保持一致
  // static const platform = const MethodChannel("com.flutter.to.android");
  var _data1 = '向01获取';
  var _data2 = '向02获取';
  var _data3 = '向03获取';
  var platforms = [];

  @override
  Widget build(BuildContext context) {
    Widget platformView() {
      if (defaultTargetPlatform == TargetPlatform.android) {
        return AndroidView(
          viewType: 'plugins.flutter.io/custom_platform_view',
          creationParams: {'text': 'Flutter传给AndroidTextView的参数'},
          onPlatformViewCreated: (id) {
            platforms.add(MethodChannel('com.flutter.to.android$id'));
          },
          creationParamsCodec: StandardMessageCodec(),
        );
      }
      return Text("没有找到安卓的view");
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.text??""),
      ),
      body: Column(children: [
        TextButton(
          child: Text('传递参数给原生View'),
          onPressed: () {
            platforms[0]
                .invokeMethod('setText', {'name': 'laomeng', 'age': 18});
          },
        ),
        SizedBox(
          height: 20,
        ),
        Row(
          mainAxisSize: MainAxisSize.min, //相当于wrap_content

          children: [
            TextButton(
                child: Text(_data1),
                onPressed: () async {
                  var result = await platforms[0]
                      .invokeMethod('getData', {'name': '李逵', 'age': 108});
                  setState(() {
                    _data1 = '${result['name']},${result['age']}';
                  });
                }),
            TextButton(
                child: Text(_data2),
                onPressed: () async {
                  var result = await platforms[1]
                      .invokeMethod('getData', {'name': '宋江', 'age': 565});
                  setState(() {
                    _data2 = '${result['name']},${result['age']}';
                  });
                }),
            TextButton(
                child: Text(_data3,),
                onPressed: () async {
                  var result = await platforms[2]
                      .invokeMethod('setText', {'name': '武松', 'age': 418});
                  setState(() {
                    _data3 = '${result['name']},${result['age']}';
                  });
                }),
          ],
        ),
        Expanded(child: Container(color: Colors.red, child: platformView())),
        Expanded(child: Container(color: Colors.blue, child: platformView())),
        Expanded(child: Container(color: Colors.yellow, child: platformView())),
      ]),
    );
  }
}
